package com.changwu.cwbase.net;


import com.changwu.cwbase.Constants;
import com.changwu.cwbase.Controllers;
import com.changwu.cwbase.bean.model.UserModel;
import com.changwu.cwbase.net.api.ApiService;
import com.changwu.cwbase.net.api.UserService;
import com.changwu.cwbase.util.LogUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/11/7.
 */

public class ApiM {


    private static final int READ_TIME_OUT = 10;
    private static final int CONNECT_TIME_OUT = 10;
    private static ApiService apiService;
    private static UserService userService;
    private static OkHttpClient okHttpClient;
    private static Converter.Factory gsonConverterFactory;
    private static CallAdapter.Factory rxJavaCallAdapterFactory;
    private static String baseUrl = Controllers.baseUrl;
    private static String updateUrl = Controllers.updateUrl;
    private static ApiM mInstance;

    static {

        okHttpClient = new OkHttpClient();
        gsonConverterFactory = GsonConverterFactory.create();
        rxJavaCallAdapterFactory = RxJava2CallAdapterFactory.create();
        //开启Log
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {

                LogUtil.e(Constants.TAG_NET, message);
            }
        });
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        Interceptor headInterceptor = new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("x-cunyibang-token", UserModel.getInstance().getToken())
                        .build();
                return chain.proceed(request);
            }
        };
        //缓存
        File cacheFile = new File("", "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb

        okHttpClient = new OkHttpClient.Builder()
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(logInterceptor)
                .addInterceptor(headInterceptor)
                .cache(cache)
                .build();

    }

    public static ApiM getInstance() {
        if (mInstance == null) {
            synchronized (ApiM.class) {
                mInstance = new ApiM();
            }
        }
        return mInstance;
    }

    public static ApiService getApiService() {
        if (apiService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(baseUrl)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            apiService = retrofit.create(ApiService.class);
        }
        return apiService;
    }

    public static UserService getUserService() {
        if (userService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(baseUrl)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            userService = retrofit.create(UserService.class);
        }
        return userService;
    }

    public static ApiService getUpdateService() {
        if (apiService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(updateUrl)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            apiService = retrofit.create(ApiService.class);
        }
        return apiService;
    }
}